#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <unordered_map>
#include <vector>
#include <set>
#include <queue>

using namespace std;
using LL = long long;
using PII = pair<int, int>;
const int N = 1e5 + 10;


int n, q;
struct Edge{
    int p, q, r;
    bool operator< (const Edge& w) const{
        return r > w.r;
    }
}edge[N];

PII query[N];
PII board[N];
int p[N], cnt[N];
map<PII, int> mp;

int cmp(PII x, PII y){
    return x.first > y.first;
}

int find(int x){
    if(x != p[x]){
        p[x] = find(p[x]);
    }
    return p[x];
}

void merge(int a, int b){
    int fa = find(a), fb = find(b);
    if(fa != fb){
        cnt[fb] += cnt[fa];
        cnt[fa] = 0;
        p[fa] = fb;
    }
}


int main(){
#ifdef LOCALFLAG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif


    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> q;
    for(int i = 1; i <= n; i ++) p[i] = i, cnt[i] = 1;
    for(int i = 0; i < n - 1; i ++){
        cin >> edge[i].p >> edge[i].q >> edge[i].r;
    }
    for(int i = 0; i < q; i ++){
        int k, v;
        cin >> k >> v;
        query[i] = {k, v};
        board[i] = {k, v};
    }

    sort(edge, edge + n - 1);
    sort(query, query + q, cmp);
    
    int fo = 0;
    for(int i = 0; i < q; i ++){
        int k = query[i].first, v = query[i].second;
        while(fo < n - 1 && edge[fo].r >= k){
            int p = edge[fo].p, q = edge[fo].q;
            merge(p, q);
            fo ++;
        }
        mp[{k, v}] = cnt[find(v)];
    }

    for(int i = 0; i < q; i ++){
        cout << mp[{board[i].first, board[i].second}] - 1 << '\n';
    }


    return 0;
}